function AddBookmark() {
  var url = 'http://dominioncards.appspot.com';
  var title = 'Dominion Card Selector';
  if (window.sidebar) {
    // Firefox
    window.sidebar.addPanel(title, url, "");
  } else if (window.opera && window.print) {
    // Opera
    var elem = document.createElement('a');
    elem.setAttribute('href', url);
    elem.setAttribute('title', title);
    elem.setAttribute('rel', 'sidebar');
    elem.click();
  } else if (document.all) {
    // IE
    window.external.AddFavorite(url, title);
  }
}

function ExpandSet(initial) {
  var expand = document.getElementById(initial + '_expand');
  var zippy = document.getElementById(initial + '_zippy');
  var rows = document.getElementsByName(initial + '_row');

  if (expand.innerHTML == 'Expand') {
    expand.innerHTML = 'Collapse';
    zippy.src = 'static/zippy_collapse.png';
    for (var i = 0; i < rows.length; ++i) {
      rows[i].style.display = '';
    }
  } else {
    expand.innerHTML = 'Expand';
    zippy.src = 'static/zippy_expand.png';
    for (var i = 0; i < rows.length; ++i) {
      rows[i].style.display = 'none';
    }
  }
}

function ShowNextCopper() {
  var i = 1;
  for (; i <= 6; ++i) {
    var player = document.getElementById('player' + i.toString());
    if (player.style.display == 'none') {
      player.style.display = '';
      break;
    }
  }
  if (i == 6) {
    document.getElementById('player_show').style.display = 'none'
  }
}

function ToggleSet(initial, set_size) {
  var checked = document.getElementById('set' + initial).checked;
  for (var i = 0; i < set_size; ++i) {
    document.getElementsByName(initial + i.toString())[0].checked = checked;
  }
}

function TakeBlackMarket(index) {
  var this_take = document.getElementById('bmt' + index.toString());
  if (this_take.disabled) {
    return;
  }

  var card_count = black_market_cards.length;
  if (this_take.value == 'Take') {
    var market_index = (black_market_offset - 3 + index + card_count) %
                       card_count;
    black_market_cards.splice(market_index, 1);
    black_market_offset = (black_market_offset - 1 + card_count) % card_count;

    for (var i = 0; i < 3; ++i) {
      var take = document.getElementById('bmt' + i.toString());
      if (i != index) {
        take.disabled = true;
      } else {
        take.disabled = false;
        take.value = 'Undo';
      }
    }
  } else {
    var this_item = document.getElementById('bm' + index.toString()).innerHTML;
    var market_index = (black_market_offset - 1 + index + card_count) %
                       (card_count + 1);
    black_market_cards.splice(market_index, 0, this_item);
    black_market_offset = black_market_offset + 1

    for (var i = 0; i < 3; ++i) {
      var take = document.getElementById('bmt' + i.toString());
      take.disabled = false;
      take.value = 'Take';
    }
  }
}

function NextBlackMarket() {
  var card_count = black_market_cards.length;
  for (var i = 0; i < Math.min(3, card_count); ++i) {
    var card = document.getElementById('bm' + i.toString());
    card.innerHTML = black_market_cards[(black_market_offset + i) % card_count];
    var take = document.getElementById('bmt' + i.toString());
    take.value = 'Take';
    take.disabled = false;
  }
  for (var i = card_count; i < 3; ++i) {
    document.getElementById('bm' + i.toString()).innerHTML = '(none)';
    var take = document.getElementById('bmt' + i.toString());
    take.value = 'Take';
    take.disabled = true;
  }
  black_market_offset = (black_market_offset + 3) % card_count;
}
